package com.itzhoun.controller;

import cn.hutool.json.JSONUtil;
import com.itzhoun.entity.UserEntity;
import com.itzhoun.listener.BaseListener;
import com.itzhoun.model.HelloApplicationEvent;
import com.itzhoun.publish.EventPublisher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 周楠
 * @Date 2022/10/30/下午 2:41
 */
@Slf4j
@RestController
@RequestMapping("event")
public class EventController {

    @Resource
    private EventPublisher eventPublisher;

    @Resource
    private ApplicationEventPublisher applicationEventPublisher;

    @Resource
    private ApplicationContext applicationContext;

    @GetMapping("/publish")
    public void publish(){
        log.info("开始执行事件监听》》》》》》》》");
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername("it周楠");
        userEntity.setAge(24);
        eventPublisher.publishHelloEvent(JSONUtil.parseObj(userEntity));
        eventPublisher.publishWatchEvent("我在看你");

        //这里两种方式监听
//        applicationEventPublisher.publishEvent(new HelloApplicationEvent(JSONUtil.parseObj(userEntity)));
//        applicationContext.publishEvent(new HelloApplicationEvent(JSONUtil.parseObj(userEntity)));
        //事实上前面也是使用了这两种方法 不过项目实际情况最好采用上方的方式去执行 分类做调整
        log.info("结束执行事件监听>>>>>>>>>>>>");

    }
}
